package edu.mis.modules.sale;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import edu.mis.model.Sale;
import edu.mis.model.Saleitem;


public class SaleController extends Controller {
	public void index() {
		render("sale.html");
	}
	public void service() {
		render("service.html");
	}
	public void list() {
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		Page<Sale> data = Sale.dao.paginate(page, rows);
		setAttr("rows", data.getList());
		setAttr("total", data.getTotalRow());
		renderJson();
	}

	public void add() throws Exception {
		Model<Sale> sale = getModel(Sale.class,"");
		if (sale.save()) {
			setAttr("success",true);  
			setAttr("msg","添加订单成功!");
		}else{
			setAttr("success",false);
			setAttr("msg","添加订单失败!");
		}
		renderJson();
	}
   
	public void edit() throws Exception {
		Model<Sale> sale = getModel(Sale.class,"");
		if (sale.update()) {
			setAttr("success",true);
			setAttr("msg","修改订单成功!");
		}else{
			setAttr("success",false);
			setAttr("msg","修改订单失败!");
		}
		renderJson();
	}
	
	/**
	 * 获得订单明细
	 */
	public void getSaleItem(){
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		String saleId = getPara("saleId");
		Page<Saleitem> data = Saleitem.dao.getSaleItem(page, rows,saleId);
		setAttr("rows", data.getList());	
		setAttr("total", data.getTotalRow());
		renderJson();
	}
	/**
	 * 添加订单产品
	 */
	public void addItem() throws Exception {
		Model<Saleitem> saleItem = getModel(Saleitem.class,"");
		saleItem.set("saleId", getParaToInt(0));
		if (saleItem.save()) {
			setAttr("success",true);  
			setAttr("msg","添加订单产品成功!");
		}else{
			setAttr("success",false);
			setAttr("msg","添加订单产品失败!");
		}
		renderJson();
	}
	/**
	 * 修改订单明细
	 */
	public void editItem() throws Exception {
		Model<Saleitem> saleItem = getModel(Saleitem.class,"");
		if (saleItem.update()) {
			setAttr("success",true);
			setAttr("msg","修改订单产品成功!");
		}else{
			setAttr("success",false);
			setAttr("msg","修改订单产品失败!");
		}
		renderJson();
	}
	/**
	 * 删除订单明细
	 */
	public void deleteItem() throws Exception {
		String ids=getPara("ids");
		if(Saleitem.dao.deleteBatch(ids)) {
			setAttr("success",true);
		    setAttr("msg","删除订单产品成功!");
		}else{
			setAttr("success",false);
			setAttr("msg","删除订单产品失败!");
		}
		renderJson();
	}
	/**
	 * 下单
	 */
	public void customItems(){
		String ids=getPara("ids");
		if(Saleitem.dao.validateState(ids, "已预订")){
			Saleitem.dao.customItems(ids);
			setAttr("success",true);  
			setAttr("msg","下单成功");
		}else{
			setAttr("success",false);  
			setAttr("msg","不能下单，请检查产品状态");
		}
		renderJson();
	}
	
	public void productTree(){
		renderJson(Db.find("SELECT p.id,p.productName FROM product p"));  
	}
	public void partnerTree(){
		renderJson(Db.find("SELECT p.id,p.partnerName FROM partner p"));  
	}
}
